package com.xs.tool.controller.api;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.xs.tool.common.base.BasicController;
import com.xs.tool.model.User;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 孙泽权
 * @email 1226112135@qq.com
 * @date 2021/4/16 上午12:04
 * @description
 */
public class ExcelController extends BasicController {
    /**
     * 导出excel
     */
    @RequestMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
//        OutputStream outputStream = response.getOutputStream();
//        String time = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss").format(new Date());
//        //添加响应头信息
//        response.setHeader("Content-disposition", "attachment; filename=" + "contract" + time + ".xlsx");
//        //设置类型
//        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
//        //设置头
//        response.setHeader("Pragma", "No-cache");
//        //设置头
//        response.setHeader("Cache-Control", "no-cache");
//        //设置日期头
//        response.setDateHeader("Expires", 0);
//
//        //获取总数据量
//        Integer count = 1000000;
//        //如果总数据量多余10万，分页导出
//        if (count > 100000) {
//            //每页多少个
//            int max = 100000;
//            //必须放到循环外，否则会刷新流
//            ExcelWriter excelWriter = EasyExcel.write(outputStream).build();
//            ;
//            for (int i = 0; i < (count / max) + 1; i++) {
//                List<User> exportList = contractService.export(i * max, max);
//                WriteSheet writeSheet = EasyExcel.writerSheet(i, "合同" + (i + 1)).head(ContractAndCompanyExcel.class)
//                        .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build();
//                excelWriter.write(exportList, writeSheet);
//            }
//
//            //刷新流
//            excelWriter.finish();
//        } else {
//            List<ContractAndCompanyExcel> exportList = contractService.export(null, null);
//            EasyExcel.write(outputStream, ContractAndCompanyExcel.class).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet("合同").doWrite(exportList);
//        }
//
//        outputStream.flush();
//        response.getOutputStream().close();

    }
}
